import java.util.ArrayList;
import java.util.Vector;

public class NorthSouthSection extends Section {
	private Intersection northern, southern;
	private Vector<Vehicle> northBound;
	private Vector<Vehicle> southBound;
	
	public NorthSouthSection (String name, Intersection northern, Intersection southern, double distance, int maxVehicles, Metric metric, String description) {
		super(name, distance, maxVehicles, metric, description);
		this.northern = northern;
		this.southern = southern;
		northBound = new Vector<Vehicle>();
		southBound = new Vector<Vehicle>();
	}
	
	/**
	 * Accessor Method (Weight)
	 * @param direction The direction of the vehicle.
	 * @return The weight of the road section.
	 */
	public double getWeight(Direction direction) {
		double weight = 0;
		
		switch (direction) {
		case North:
			weight = metric.calculateWeight(distance, northBound.size());
			break;
		case South:
			weight = metric.calculateWeight(distance, southBound.size());
			break;
		}
		
		return weight;
	}
	
	/**
	 * Accessor Method (Northern Intersection)
	 * @return	The intersection to the north of the road section.
	 */
	public Intersection getNorthernIntersection() {
		return northern;
	}
	
	/**
	 * Accessor Method (Southern Intersection)
	 * @return	The intersection to the south of the road section.
	 */
	public Intersection getSouthernIntersection() {
		return southern;
	}
	
	/**
	 * Adds a vehicle in either the north or south bound direction.
	 * Checks to see if traffic has not exceeded maxVehicles.
	 * @param vehicle 		The vehicle to be added.
	 * @param direction 	The direction in which to add the vehicle.
	 * @return True/false if vehicle could be added.
	 */
	public boolean addVehicle(Vehicle vehicle, Direction direction) {
		boolean success = false;

		switch (direction) {
		case North:
			if (northBound.size() < maxVehicles) {
				northBound.addElement(vehicle);
				success = true;
			}
			break;
		case South:
			if (southBound.size() < maxVehicles) {
				southBound.addElement(vehicle);
				success = true;
			}
			break;
		}

		return success;
	}

	public boolean addVehiclewithoutMax(Vehicle vehicle, Direction direction) {
		boolean success = false;

		switch (direction) {
		case North:
				northBound.addElement(vehicle);
				success = true;
			break;
		case South:
				southBound.addElement(vehicle);
				success = true;
			break;
		}

		return success;
	}

	/**
	 * Removes a vehicle from either the north or south bound direction.
	 * @param vehicle 		The vehicle to be removed.
	 * @param direction 	The direction in which to remove the vehicle.
	 * @return True/false if vehicle could be removed.
	 */
	public boolean removeVehicle(Vehicle vehicle, Direction direction) {
		boolean success = false;

		switch (direction) {
		case North:
			success = northBound.removeElement(vehicle);
			break;
		case South:
			success = southBound.removeElement(vehicle);
			break;
		}

		return success;
	}
	
	/**
	 * Returns the next intersection for this road given the last intersection and current direction
	 * @param direction 			The direction of the vehicle
	 * @return The next intersection the vehicle will hit.
	 */
	public Intersection nextIntersection(Direction direction) {
		Intersection temp = null;
		
		switch (direction) {
			case North:
				temp = northern;
			break;
			case South:
				temp = southern;
			break;
		}

		return temp;
	}
	
	
	/***
	 * Returns the list of Intersection for the current section
	 * @param 
	 * @return A list of 2 intersections for the section
	 */
	public ArrayList<Intersection> getIntersections() {
		ArrayList<Intersection> intersections = new ArrayList<Intersection>();
		intersections.add(northern);
		intersections.add(southern);
		return intersections;
	}
	
	public int getSize(Direction dir){
		switch (dir) {
		case North:
			return northBound.size();
		case South:
			return southBound.size();
	}

		return 1000;
	}
}